package cn.iocoder.yudao.module.system.service.officeinfo;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.officeinfo.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.officeinfo.OfficeinfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.officeinfo.SeatinfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.officeinfo.OfficeinfoMapper;
import cn.iocoder.yudao.module.system.dal.mysql.officeinfo.SeatinfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 办公室信息 Service 实现类
 *
 * @author Warren365
 */
@Service
@Validated
public class OfficeinfoServiceImpl implements OfficeinfoService {

    @Resource
    private OfficeinfoMapper officeinfoMapper;
    @Resource
    private SeatinfoMapper seatinfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createOfficeinfo(OfficeinfoSaveReqVO createReqVO) {
        // 插入
        OfficeinfoDO officeinfo = BeanUtils.toBean(createReqVO, OfficeinfoDO.class);
        officeinfoMapper.insert(officeinfo);

        // 插入子表
        createSeatinfoList(officeinfo.getId(), createReqVO.getSeatinfos());
        // 返回
        return officeinfo.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOfficeinfo(OfficeinfoSaveReqVO updateReqVO) {
        // 校验存在
        validateOfficeinfoExists(updateReqVO.getId());
        // 更新
        OfficeinfoDO updateObj = BeanUtils.toBean(updateReqVO, OfficeinfoDO.class);
        officeinfoMapper.updateById(updateObj);

        // 更新子表
        updateSeatinfoList(updateReqVO.getId(), updateReqVO.getSeatinfos());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOfficeinfo(Long id) {
        // 校验存在
        validateOfficeinfoExists(id);
        // 删除
        officeinfoMapper.deleteById(id);

        // 删除子表
        deleteSeatinfoByOfficeId(id);
    }

    private void validateOfficeinfoExists(Long id) {
        if (officeinfoMapper.selectById(id) == null) {
            throw exception(OFFICEINFO_NOT_EXISTS);
        }
    }

    @Override
    public OfficeinfoDO getOfficeinfo(Long id) {
        return officeinfoMapper.selectById(id);
    }

    @Override
    public PageResult<OfficeinfoDO> getOfficeinfoPage(OfficeinfoPageReqVO pageReqVO) {
        return officeinfoMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（办公室座位信息） ====================

    @Override
    public List<SeatinfoDO> getSeatinfoListByOfficeId(Long officeId) {
        return seatinfoMapper.selectListByOfficeId(officeId);
    }

    @Override
    public List<OfficeinfoDO> selectList(OfficeinfoPageReqVO Vo) {
        return officeinfoMapper.selectList(Vo);
    }

    private void createSeatinfoList(Long officeId, List<SeatinfoDO> list) {
        list.forEach(o -> o.setOfficeId(officeId));
        seatinfoMapper.insertBatch(list);
    }

    private void updateSeatinfoList(Long officeId, List<SeatinfoDO> list) {
        deleteSeatinfoByOfficeId(officeId);
		list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下：1）id 冲突；2）updateTime 不更新
        createSeatinfoList(officeId, list);
    }

    private void deleteSeatinfoByOfficeId(Long officeId) {
        seatinfoMapper.deleteByOfficeId(officeId);
    }

}